﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace WindowsService
{
    public static class Mailer
    {
        public enum Mode
        {
            Startup,
            Stopped,
            Shutdown
        }

        public static void SendMail(Mode mode)
        {
            // Wait
            Thread.Sleep(30000);
            
            SmtpClient smtpClient = new SmtpClient();
            NetworkCredential basicCredential =
                new NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["MailUser"], System.Configuration.ConfigurationManager.AppSettings["MailPass"]);
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["MailFrom"]);

            smtpClient.Host = System.Configuration.ConfigurationManager.AppSettings["MailHost"];
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = basicCredential;
            smtpClient.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MailPort"]);
            

            message.From = fromAddress;
            message.IsBodyHtml = true;

            if (mode == Mode.Startup)
            {
                message.Subject = System.Configuration.ConfigurationManager.AppSettings["MailSubjectStartup"];
                message.Body = System.Configuration.ConfigurationManager.AppSettings["MailTextStartup"];
            }

            if (mode == Mode.Shutdown)
            {
                message.Subject = System.Configuration.ConfigurationManager.AppSettings["MailSubjectShutdown"];
                message.Body = System.Configuration.ConfigurationManager.AppSettings["MailTextShutdown"];
            }

            if (mode == Mode.Stopped)
            {
                message.Subject = System.Configuration.ConfigurationManager.AppSettings["MailSubjectStop"];
                message.Body = System.Configuration.ConfigurationManager.AppSettings["MailTextStop"];
            }

            message.To.Add(System.Configuration.ConfigurationManager.AppSettings["MailTo"]);

            // Placeholder
            message.Subject = Placeholder(message.Subject);
            message.Body = Placeholder(message.Body);

            try
            {
                smtpClient.Send(message);
            }
            catch (Exception ex)
            {
                //Error, could not send the message
                //Response.Write(ex.Message);
            }
        }

        private static string Placeholder(string text)
        {
            string hostname = System.Environment.MachineName;

            try
            {
                TimeSpan uptime = GetUpTime();
                text = text.Replace("@@uptime@@", uptime.ToString());
            }
            catch (Exception)
            {
                
                throw;
            }
            

            text = text.Replace("@@hostname@@", hostname);
            text = text.Replace("@@datetime@@", DateTime.Now.ToString());

            return text;
        }

        public static TimeSpan GetUpTime()
        {
            return TimeSpan.FromMilliseconds(GetTickCount64());
        }

        [DllImport("kernel32")]
        extern static UInt64 GetTickCount64();


    }
}
